//
//  FSWHomeController.m
//  quhuolang_service
//
//  Created by 曹欣欣 on 2018/2/9.
//  Copyright © 2018年 fusw. All rights reserved.
//

#import "FSWHomeController.h"
#import "FSWHomeLogisticsTableCell.h"
#import "FSWMyController.h"
#import "LoginController.h"
#import "MainNavigationVC.h"
#import "FSWBillDetailController.h"
#import "FSWSelectBottomView.h"
#import "FSWSearchModel.h"
#import "FSWEmptyView.h"
#import "FSWBillListModel.h"
#import "FSWBaseWebViewController.h"
#import <UMPush/UMessage.h>
#import "BRPickerView.h"


@interface FSWHomeController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray* bannersArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *searchBackView;

@property (weak, nonatomic) IBOutlet UIButton *startDateBtn;
@property (weak, nonatomic) IBOutlet UIButton *endDateBtn;
@property (weak, nonatomic) IBOutlet UILabel *startDateL;
@property (weak, nonatomic) IBOutlet UILabel *endDateL;

@property (weak, nonatomic) IBOutlet UITextField *orderF;
@property (weak, nonatomic) IBOutlet UITextField *keywordsF;

@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (weak, nonatomic) IBOutlet UIButton *dzBtn;
@property (weak, nonatomic) IBOutlet UIButton *shopBtn;
@property (weak, nonatomic) IBOutlet UILabel *payL;
@property (weak, nonatomic) IBOutlet UILabel *dzL;
@property (weak, nonatomic) IBOutlet UILabel *shopL;

@property (assign, nonatomic) Boolean isShowSearch;

@property (nonatomic, strong)NSMutableArray* listArray;

@property (nonatomic, strong)NSMutableArray* payListArray;
@property (nonatomic, strong)NSMutableArray* dzListArray;
@property (nonatomic, strong)NSMutableArray* shopListArray;

@property (nonatomic, strong) FSWSearchModel *smodel;

@property (strong, nonatomic) BRDatePickerView *datePickerView;
@property (strong, nonatomic) FSWSelectBottomView *selectView;


@end

@implementation FSWHomeController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    NSString *access_token = [fUserDefaults objectForKey:ACCESS_TOKEN];
    if (access_token.length > 0) {
        [self loadBillData];
    }else{
//        LoginController *vc = [self instantiateViewControllerWithStoryboardName:@"Login" identifier:@"LoginController"];
//        [self pushViewController:vc];
        [self loadNotBillData];
    }
    [self configSearchUI];
     
    [fNotification addObserver:self selector:@selector(getNotificationResult:) name:@"message" object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [fNotification removeObserver:self name:@"message" object:nil];
}
-(void)getNotificationResult:(NSNotification *)notification{
    NSDictionary *dict = notification.object;
    NSDictionary *aps = dict[@"aps"];
    NSString *message = aps[@"alert"];
    NSInteger code = [dict[@"code"] integerValue];
    [self configJPush:code andMessage:message];
}


-(void)configJPush:(NSInteger)code andMessage:(NSString *)message{
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账单列表";
    
    NSString *access_token = [fUserDefaults objectForKey:ACCESS_TOKEN];
    if (access_token.length > 0) {
        [self loadBillData];
    }else{
        LoginController *vc = [self instantiateViewControllerWithStoryboardName:@"Login" identifier:@"LoginController"];
        [self pushViewController:vc];
    }
    
    @weakify(self);
    [self setNavigationLeftBarItemType:OnlyHaveTitle title:@"筛选" titleFont:15 titleColor:[UIColor whiteColor] image:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] responderBlock:^{
        @strongify(self)
        self.isShowSearch = !self.isShowSearch;
        if(self.isShowSearch){
            self.searchBackView.hidden = NO;
        }else{
            self.searchBackView.hidden = YES;
        }
    }];
    [self setNavigationRightBarItemType:OnlyHaveTitle title:@"设置" titleFont:15 titleColor:[UIColor whiteColor] image:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] responderBlock:^{
        @strongify(self)
        FSWMyController *vc = [self instantiateViewControllerWithStoryboardName:@"My" identifier:@"FSWMyController"];
        [self pushViewController:vc];
        
      
    }];
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }
    self.tableView.mj_header = [self headerGifRefreshBlock:^{
        @strongify(self);
        NSString *access_token = [fUserDefaults objectForKey:ACCESS_TOKEN];
        if (access_token) {
         
        }
        [self loadBillData];
        [self.tableView.mj_header endRefreshing];
    }];
    self.smodel = [[FSWSearchModel alloc]init];
    self.searchBackView.hidden = YES;
    
    self.datePickerView = [[BRDatePickerView alloc]init];
    self.datePickerView.title = @"选择日期";
    self.datePickerView.isAutoSelect = NO;
    self.datePickerView.maxDate = [NSDate date];
    
 
    
    //绑定别名
    [UMessage addAlias:@"yudi" type:@"UMENGTEST" response:^(id  _Nonnull responseObject,NSError*_Nonnull error){
    }];
    
    
    //    监听密码输入
    [[self.orderF rac_textSignal] subscribeNext:^(NSString* x) {
        @strongify(self)
        self.smodel.orderNo = x;
    }];
    
    //    监听密码输入
    [[self.keywordsF rac_textSignal] subscribeNext:^(NSString* x) {
        @strongify(self)
        self.smodel.keywords = x;
    }];
    
    [self loadBaseData];
    
   
}

-(void)loadBaseData{
    [self.payListArray removeAllObjects];
    [self.dzListArray removeAllObjects];
    [self.shopListArray removeAllObjects];
    
    FSWSelectModel *pmodel1 = [[FSWSelectModel alloc]init];
    pmodel1.name = @"未支付";
    pmodel1.id = @"1";
    FSWSelectModel *pmodel2 = [[FSWSelectModel alloc]init];
    pmodel2.name = @"已支付";
    pmodel2.id = @"2";
    FSWSelectModel *pmodel3 = [[FSWSelectModel alloc]init];
    pmodel3.name = @"其他";
    pmodel3.id = @"3";
    [self.payListArray addObject:pmodel1];
    [self.payListArray addObject:pmodel2];
    [self.payListArray addObject:pmodel3];
    
    FSWSelectModel *dmodel1 = [[FSWSelectModel alloc]init];
    dmodel1.name = @"未到账";
    dmodel1.id = @"1";
    FSWSelectModel *dmodel2 = [[FSWSelectModel alloc]init];
    dmodel2.name = @"已到账";
    dmodel2.id = @"2";
    FSWSelectModel *dmodel3 = [[FSWSelectModel alloc]init];
    dmodel3.name = @"其他";
    dmodel3.id = @"3";
    [self.dzListArray addObject:dmodel1];
    [self.dzListArray addObject:dmodel2];
    [self.dzListArray addObject:dmodel3];
    
    FSWSelectModel *smodel1 = [[FSWSelectModel alloc]init];
    smodel1.name = @"张三";
    smodel1.id = @"1";
    FSWSelectModel *smodel2 = [[FSWSelectModel alloc]init];
    smodel2.name = @"李四";
    smodel2.id = @"2";
    FSWSelectModel *smodel3 = [[FSWSelectModel alloc]init];
    smodel3.name = @"微克";
    smodel3.id = @"3";
    FSWSelectModel *smodel4 = [[FSWSelectModel alloc]init];
    smodel4.name = @"刘莉莉";
    smodel4.id = @"4";
    FSWSelectModel *smodel5 = [[FSWSelectModel alloc]init];
    smodel5.name = @"胡一鸥";
    smodel5.id = @"5";
    [self.shopListArray addObject:smodel1];
    [self.shopListArray addObject:smodel2];
    [self.shopListArray addObject:smodel3];
    [self.shopListArray addObject:smodel4];
    [self.shopListArray addObject:smodel5];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    @weakify(self);
    FSWHomeLogisticsTableCell* cell = [tableView dequeueReusableCellWithIdentifier:@"listCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FSWBillDetailController *vc = [self instantiateViewControllerWithStoryboardName:@"Home" identifier:@"FSWBillDetailController"];
    [self pushViewController:vc];
}

-(void) loadBillData{
    NSArray *array = @[@{@"id":@"1",@"name":@"扫码查询",@"pic":@"home_saoma"},@{@"id":@"1",@"name":@"货物发货",@"pic":@"huowufahuo"},@{@"id":@"1",@"name":@"联系客服",@"pic":@"lianxiwomen"}];
    
    NSArray *listModel = [FSWBillListModel mj_objectArrayWithKeyValuesArray:array];
    
    [self.listArray removeAllObjects];
    [self.listArray addObjectsFromArray:listModel];
    [self.tableView reloadData];
    [self emptyView:1];
}
-(void) loadNotBillData{
    [self.listArray removeAllObjects];
    [self emptyView:1];
}

- (IBAction)startDateButton:(id)sender {
    [self showDateUI:1];
     
}

- (IBAction)endDateButton:(id)sender {
    
    [self showDateUI:2];
    
}

-(void)showDateUI:(NSInteger)type{
    self.datePickerView.pickerMode = BRDatePickerModeYMD;
//    NSString *datestr = [NSString stringWithFormat:@"%@-01-01",[FSWHelperTool getDataOneYearOrMonthOrDay:0]];
    NSString *datestr = [NSString stringWithFormat:@"%@-01-01",@"2020"];
    self.datePickerView.minDate = [FSWHelperTool getStrToDate:datestr andFormatter:@"YYYY-MM-dd"];
    @weakify(self);
    self.datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        @strongify(self);
        NSDate *date = [FSWHelperTool getStrToDate:selectValue andFormatter:@"YYYY-MM-dd"];
        NSString *yyyy =  [FSWHelperTool getDataOneYearOrMonthOrDay:date andNumber:0];
        NSString *mm =  [FSWHelperTool getDataOneYearOrMonthOrDay:date andNumber:1];
        NSString *dd =  [FSWHelperTool getDataOneYearOrMonthOrDay:date andNumber:2];
        NSString *str = [NSString stringWithFormat:@"%@-%@-%@",yyyy,mm,dd];
        if(type == 1){
            self.smodel.startDate = str;
        }else{
            self.smodel.endDate = str;
        }
        [self configSearchUI];
    };
    [self.datePickerView show];
}
- (IBAction)payButton:(id)sender {
    [self showSelectView:1];
}
- (IBAction)dzButton:(id)sender {
    [self showSelectView:2];
}
- (IBAction)shopButton:(id)sender {
    [self showSelectView:3];
}
- (IBAction)clearButton:(id)sender {
    self.smodel = [[FSWSearchModel alloc]init];
    [self configSearchUI];
}
- (IBAction)searchButton:(id)sender {
    [self loadBillData];
}


-(void)showSelectView:(NSInteger)type{
    self.selectView = [FSWSelectBottomView footerView];
    self.selectView.isnil = 1;
    self.selectView.type = 1;
    self.selectView.type = 0;
   
    switch (type) {
        case 1:{
            self.selectView.titleStr = @"支付类型";
            [self.selectView show:self.payListArray];
           
            break;
        }
        case 2:{
            self.selectView.titleStr = @"到账类型";
            [self.selectView show:self.dzListArray];
         
            break;
        }
        case 3:{
            self.selectView.titleStr = @"商户名称";
            [self.selectView show:self.shopListArray];
           
            break;
        }
        default:
            break;
    }
    @weakify(self);
    self.selectView.selectBlock = ^(NSMutableArray * _Nonnull parray) {
        
        NSMutableArray *narray = [NSMutableArray array];
        NSMutableArray *iarray = [NSMutableArray array];
        for (int i = 0; i<parray.count; i++) {
            FSWSelectModel *pmodel = parray[i];
            [narray addObject:pmodel.name];
            [iarray addObject:pmodel.id];
        }
        NSString *ids = [iarray componentsJoinedByString:@","];
        NSString *names = [narray componentsJoinedByString:@","];
        @strongify(self);
        switch (type) {
            case 1:{
                self.smodel.payStatus = ids;
                self.smodel.payStatusName = names;
                break;
            }
            case 2:{
                self.smodel.accountStatus = ids;
                self.smodel.accountStatusName = names;
                break;
            }
            case 3:{
                self.smodel.shopId = ids;
                self.smodel.shopName = names;
                break;
            }
            default:
                break;
        }
        [self configSearchUI];
        [self.tableView reloadData];
    };
}
    
-(void)configSearchUI{
    self.startDateL.text = [FSWHelperTool isBlankToString:self.smodel.startDate andToString:@""];
    self.endDateL.text = [FSWHelperTool isBlankToString:self.smodel.endDate andToString:@""];
    self.payL.text = [FSWHelperTool isBlankToString:self.smodel.payStatusName andToString:@"付款状态"];
    self.dzL.text = [FSWHelperTool isBlankToString:self.smodel.accountStatusName andToString:@"到账状态"];
    self.shopL.text = [FSWHelperTool isBlankToString:self.smodel.shopName andToString:@"商户名称"];
    self.orderF.text = self.smodel.orderNo;
    self.keywordsF.text = self.smodel.keywords;
    
}

-(void)emptyView:(NSInteger)type{
   @weakify(self);
//    0 取消 2 无数据 3 网络请求错误
  switch (type) {
      case 0:{
          [FSWEmptyView hiddenView:self.tableView];
          break;
      }
      case 1:{
          if(self.listArray.count <=0){
              [FSWEmptyView show:NoData andShowButton:NO andMsg:@"暂无账单信息" andSuperView:self.tableView andBlock:^{
                  
              } andAddButtonBlock:^{
                  @strongify(self);
                  [self loadBillData];
              }];
              
            
          }else{
              [FSWEmptyView hiddenView:self.tableView];
          }
          break;
      }
      case 2:{
          [FSWEmptyView show:NetError andShowButton:YES andMsg:@"" andSuperView:self.tableView andBlock:^{
              
          } andAddButtonBlock:^{
              @strongify(self);
              [self loadBillData];
          }];
          break;
      }
      default:
          break;
  }
  [self.tableView reloadData];
}


#pragma mark - Setter/Getter
- (NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (NSMutableArray *)payListArray
{
    if (!_payListArray) {
        _payListArray = [NSMutableArray array];
    }
    return _payListArray;
}

- (NSMutableArray *)dzListArray
{
    if (!_dzListArray) {
        _dzListArray = [NSMutableArray array];
    }
    return _dzListArray;
}

- (NSMutableArray *)shopListArray
{
    if (!_shopListArray) {
        _shopListArray = [NSMutableArray array];
    }
    return _shopListArray;
}

@end


